package com.laikan.reader.bookfriend.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.TextView;
import com.laikan.library.utils.DeviceUtil;
import com.laikan.reader.R;
import com.tencent.mobileqq.openpay.api.IOpenApi;
import com.tencent.mobileqq.openpay.api.IOpenApiListener;
import com.tencent.mobileqq.openpay.api.OpenApiFactory;
import com.tencent.mobileqq.openpay.data.base.BaseResponse;
import com.tencent.mobileqq.openpay.data.pay.PayResponse;

public class QQWalletCallbackActivity extends Activity implements IOpenApiListener {

    //    String appId = "100703379";
    private String appId = "1105459927";
    private IOpenApi openApi;
    private TextView text_back,text_msg;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_callback);
        text_back = (TextView)findViewById(R.id.text_back);
        text_msg = (TextView)findViewById(R.id.text_msg);
        openApi = OpenApiFactory.getInstance(this, appId);
        openApi.handleIntent(getIntent(), this);
        text_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
                QQWalletCallbackActivity.this.overridePendingTransition(0, R.anim.close);;

            }
        });

    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        openApi.handleIntent(intent, this);
    }

    @Override
    public void onOpenResponse(BaseResponse response) {
        String title = "Callback from mqq";
        String message;

        if (response == null) {
            message = "response is null.";
            return;
        }else {
            if (response instanceof PayResponse) {
                PayResponse payResponse = (PayResponse)response;

                message = " apiName:" + payResponse.apiName
                          + " serialnumber:" + payResponse.serialNumber
                          + " isSucess:" + payResponse.isSuccess()
                          + " retCode:" + payResponse.retCode
                          + " retMsg:" + payResponse.retMsg;
                if (payResponse.isSuccess()) {
                    if (!payResponse.isPayByWeChat()) {
                        message += " transactionId:" + payResponse.transactionId
                                   + " payTime:" + payResponse.payTime
                                   + " callbackUrl:" + payResponse.callbackUrl
                                   + " totalFee:" + payResponse.totalFee;
//                                   + " spData:" + payResponse.spData;
                        text_msg.setText(DeviceUtil.getHtmlFormatedString(QQWalletCallbackActivity.this, R.string.qqwallet_total_fee, "成功充值K币", "x"+payResponse.totalFee));
                        System.out.println(message);
                    }

                }else{
                    finish();
                }
            }else {
                message = "response is not PayResponse.";
            }
        }

//        Dialog alertDialog = new AlertDialog.Builder(this).
//            setTitle(title).
//            setMessage(message).
//            create();
//        alertDialog.show();
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
//            this.overridePendingTransition(R.anim.close, 0);
            finish();
        }
        return true;
    }


    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(0, R.anim.close);
    }
    @Override
    protected void onDestroy() {
        this.overridePendingTransition(R.anim.close, 0);
        super.onDestroy();
    }
}
